package com.qf.mrmd.controller;

import com.alibaba.fastjson.JSONObject;
import com.qf.mrmd.dao.PicDao;
import com.qf.mrmd.entity.Pic;
import com.qf.mrmd.result.ResponseCode;
import com.qf.mrmd.result.ResponseData;
import com.qf.mrmd.service.PicService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;

/*
get post delete patch put restful设计风格
模拟请求http://localhost:8080/pics/query?id=1&url=qingwa
*不要再使用动词来描述API
*http://localhost:8080/pics/  删除  DELEETE
*http://localhost:8080/pics/   添加  POST
 *http://localhost:8080/pics/  修改 PUT PATCH
 其他请求放在请求体body {"id":1,"title":"nihao"} ---json

 http://localhost:8080/pics/1/nihao  查询 GET
 */

@Api("PicApis")
//@Controller  //控制层注解
//@ResponseBody
@RestController  //组合注解 ，Controller和ResponseBody的组合
@RequestMapping("pics")  //路径映射
public class PicController {

    @Autowired //自动装配 类型进行匹配
//    @Resource //通过名字进行匹配，较粗糙
    private PicService picService;

    @Resource
    private PicDao picDao;


    //通过id查询图片 get
    @ApiOperation(value = "查询图片",notes="通过id查询图片")
    @ApiImplicitParams({ //描述多个参数
            @ApiImplicitParam(name = "id",value = "图片的id",dataType = "integer"),
            @ApiImplicitParam(name = "title",value = "图片的标题")
    })
    //@RequestMapping(value = "/{id}/{title}",method = RequestMethod.GET)
    @GetMapping("/{id}")
    public ResponseData query(@PathVariable Integer id){
        Pic pic = picService.queryById(id);
        return new ResponseData(ResponseCode.SUCCESS,pic);
    }

    //添加图片
    @ApiOperation(value="添加图片")
    //http://localhost:8080/pics
    //{"pId“:1，"title":"lb","url":"http://localhost:8080/lb.jpg"}
    @PostMapping("/in")
    public ResponseData insert(@RequestBody Pic pic){
        //@RequestBody只接收一个参数
        Pic pic1 = picService.insert(pic);
        return new ResponseData(ResponseCode.SUCCESS,pic);
    }

    @ApiIgnore
    //{"pId“:1，"title":"lb","url":"http://localhost:8080/lb.jpg", "uu":"卢本伟"}
    @PostMapping("/v1")
    public String upPic1(@RequestBody String s){
        //将s转换成json对象
        //再通过json对象进行解析
        JSONObject jsonObject = JSONObject.parseObject(s);
        Integer pId = jsonObject.getInteger("pId");
        String uu = jsonObject.getString("uu");
        return pId+"-----"+uu;
    }

    //@DeleteMapping("/")
    @PutMapping("/v1")
    public String upPic2(@RequestBody String s){
        //将s转换成json对象
        //再通过json对象进行解析
        //updete pic set title="",url="",type="" where id=1
        JSONObject jsonObject = JSONObject.parseObject(s);
        Integer pId = jsonObject.getInteger("pId");
        String uu = jsonObject.getString("uu");
        return pId+"-----"+uu;
    }

    @PatchMapping("/v1")
    public String upPic3(@RequestBody String s){
        //将s转换成json对象
        //再通过json对象进行解析
        JSONObject jsonObject = JSONObject.parseObject(s);
        Integer pId = jsonObject.getInteger("pId");
        String uu = jsonObject.getString("uu");
        return pId+"-----"+uu;
    }


//    @RequestMapping("/del")
////    @ResponseBody  //将返回值自动转换成json
//    public String delete(){
//        System.out.println("成功接收到了请求");
//        return "nihao";
//    }
//
//    @RequestMapping("/query")
//    public Pic query(Pic pic,String uu){
//        Pic pic1 = picService.queryById(pic.getpId());
//        return pic1;
//    }
//
//    @RequestMapping("/insert")
//    public void insert(){
//        System.out.println("杀戮中绽放");
//
//    }


    /*
    * 完成一次完整的添加操作
    * 1.http://localhost:8080/pics/    ----POST
    * 2.{"title":"轮播","url":"http://localhost:88./img/a.jpg","type":"lb"}
    * 3.返回ResponseData {ecode:"",info:"",data:""}
    * */
    @ApiOperation(value = "保存图片信息")
    @PostMapping("/")
    public ResponseData savePicInfo(@RequestBody Pic pic){
        ResponseData responseData = picService.savePicInfo(pic);
        return responseData;
    }

    @ApiOperation(value = "获取轮播图或导航图",notes = "通过图片类型，获取对应的图片列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type",value = "图片类型",dataType = "string"),
            @ApiImplicitParam(name = "status",value = "图片状态",dataType = "integer")
    })
    @GetMapping("/all/{type}/{status}")
    public ResponseData picInfo(@PathVariable String type,@PathVariable Integer status) {

        return picService.queryByType(type,status);
    }


    //作业
    //查询所有图片
    @GetMapping("/work")
    public ResponseData selectWork(){
        return picService.queryAllWork();
    }
    //修改图片
    @PostMapping("/up/{id}")
    public ResponseData updateWork(@PathVariable Integer id){
        ResponseData responseData = picService.updateWork(id);
        return responseData;
    }
    //插入图片
    @PostMapping("/work/in")
    public ResponseData insertWork(@RequestBody Pic pic){
       ResponseData responseData = picService.insertWork(pic);
       return responseData;
    }
    //删除图片(通过id删除)
    @PostMapping("/del/{id}")

    public ResponseData delWork(@PathVariable Integer id){
        ResponseData pic = picService.delWork(id);
        return pic;
    }









}

